﻿
/* ----- DrawingArea -----*/
(function () {
    // Конструктор класса DrawingArea
    //
    monitor.DrawingArea = function (options) {
        this.map = options.map;
        var self = this;
        this.eventListener = null;

        this.areaObj = {
            areaPoly: null,
            hintPolyline: null,
            vertexCount: 0,
            stVert: null,
            endVert: null,
        };

        init();

        function createPolyline() {
            var lineOpts = {
                map: self.map, // обязательный параметр
                color: globalConfig.polylineOptions.color,
                opacity: globalConfig.polylineOptions.opacity,
                weight: globalConfig.polylineOptions.weight
            };
            return new monitor.gmap.Polyline(lineOpts);
        }
        function createPolygon() {
            var areaOpts = {
                map: self.map, // обязательный параметр
                borderColor: globalConfig.polygonOptions.borderColor,
                borderOpacity: globalConfig.polygonOptions.borderOpacity,
                borderWeight: globalConfig.polygonOptions.borderWeight,
                fillColor: globalConfig.polygonOptions.fillColor,
                fillOpacity: globalConfig.polygonOptions.fillOpacity,
                editable: true,
            };
            return new monitor.gmap.AreaPoly(areaOpts);
        }

        // Функция инициализации oбъекта DrawingArea. 
        function init() {
            self.areaObj.areaPoly = createPolygon();
            self.areaObj.areaPoly.show();
            self.areaObj.hintPolyline = createPolyline();
            self.eventListener = monitor.gmap.event.eventListener();
            self.map.on('click', function (mouseEvent) {
                self.addNextVertex(mouseEvent.coord);
            });
            self.map.on('mousemove', function (mouseEvent) {
                self.moveHintLine(mouseEvent.coord);
            });
            self.areaObj.hintPolyline.on('click', function (mouseEvent) {
                //self.map.trigger('click', mouseEvent);
                self.addNextVertex(mouseEvent.coord);
            });
            self.areaObj.areaPoly.on('mousemove', function (mouseEvent) {
                //self.map.trigger('click', mouseEvent);
                self.moveHintLine(mouseEvent.coord);
            });
        }
    };

    monitor.DrawingArea.prototype = {
        // Добавление очередной вершины в рисуемую область.
        addNextVertex: function (vertLatLng) {
            //if (this.isAdding) {
            //this.stVert = vertLatLng;

            if (this.areaObj.areaPoly) {
                this.areaObj.areaPoly.addVertex(vertLatLng);
                if (this.areaObj.vertexCount++ == 0) {
                    this.areaObj.stVert = vertLatLng;
                }
                this.areaObj.endVert = vertLatLng;
                // this.areaObj.vertexCount++;
            }

            /*if (this.areaObj.dottedLine) {
                var dottedPath = this.areaObj.dottedLine.getPath();
                if (dottedPath.length >= 2) {
             this.areaObj.dottedLine.setPath([]);
                } else {
             dottedPath.push(vertLatLng);
                }
            };*/
            //};
        },
        // Перерисовать линию, изображающую то, как будет выглядеть область
        // после добавления очередной вершины.
        moveHintLine: function (currVert) {
            if (this.areaObj.areaPoly) {
                /*if(this.areaObj.vertexCount == 1) {
                    this.areaObj.hintPolyline.setPath([]);
                    var path = this.areaObj.hintPolyline.getPath();
                    path.push(this.areaObj.stVert);
                    path.push(currVert);
                } else*/
                if (this.areaObj.vertexCount > 0) {
                    //this.areaObj.hintPolyline.setPath([]);
                    this.areaObj.hintPolyline.erase();
                    //var path = this.areaObj.hintPolyline.getPath();
                    //path.push(this.areaObj.stVert);
                    //path.push(currVert);
                    //path.push(this.areaObj.endVert);
                    this.areaObj.hintPolyline.addVertex(this.areaObj.stVert);
                    this.areaObj.hintPolyline.addVertex(currVert);
                    this.areaObj.hintPolyline.addVertex(this.areaObj.endVert);
                }
            }
        },
        getCoords: function () {
            var coordsArray = [];
            if (this.areaObj.areaPoly) {
                coordsArray = this.areaObj.areaPoly.getCoords();
            }
            return coordsArray;
        },
        onDrawingMode: function () {
            var self = this;
            this.map.on('click', function (mouseEvent) {
                self.addNextVertex(mouseEvent.coord);
            });
            this.map.on('mousemove', function (mouseEvent) {
                self.moveHintLine(mouseEvent.coord);
            });
        },
        offDrawingMode: function () {
            this.map.off('click');
            this.map.off('mousemove');
            this.areaObj.hintPolyline.erase();
            this.areaObj.vertexCount = 0;
        },
        stop: function () {
            this.map.off('click');
            this.map.off('mousemove');
            this.areaObj.hintPolyline.erase();
            this.areaObj.vertexCount = 0;
        },
        clearOverlays: function (clearList) {
            this.areaObj.areaPoly.clearArea();
            this.areaObj.hintPolyline.erase();
            this.areaObj.vertexCount = 0;
            this.areaObj.stVert = {};
            this.areaObj.endVert = {};
        },
        on: function (eventName, handler) {
            this.eventListener.on(eventName, handler);
        },
        off: function (eventName, handler) {
            this.eventListener.off(eventName, handler);
        },
        trigger: function (eventName) {
            this.eventListener.trigger(eventName);
        },
    };



    /*  
     // Показ выбранной области на всю карту.
     DrawingArea.prototype.fitArea = function(area) {
         var path = area.getPath();
         var latDown = path.getAt(0).lat();
         var latUp = latDown;
         var lngLeft = path.getAt(0).lng();
         var lngRight = lngLeft;
         for (var i = 0; i < path.length; i++) {
             var latlng = path.getAt(i);
             if (latlng.lat() < latDown) {
                 latDown = latlng.lat();
             } else if (latlng.lat() > latUp) {
                 latUp = latlng.lat();
             }
             if (latlng.lng() < lngLeft) {
                 lngLeft = latlng.lng();
             } else if (latlng.lng() > lngRight) {
                 lngRight = latlng.lng();
             }
         }
 
         var bounds = new google.maps.LatLngBounds(
             new google.maps.LatLng(latDown, lngLeft),
             new google.maps.LatLng(latUp, lngRight));
 
         this.map.fitBounds(bounds);
     };*/

})();